package top.lyc829.iot_android.fragment;

import androidx.appcompat.widget.Toolbar;
import androidx.lifecycle.ViewModelProvider;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import androidx.viewpager2.widget.ViewPager2;

import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.SPUtils;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.xuexiang.xpage.annotation.Page;
import com.xuexiang.xpage.base.XPageFragment;
import com.xuexiang.xui.adapter.FragmentAdapter;
import com.xuexiang.xui.widget.actionbar.TitleBar;
import com.xuexiang.xutil.common.CollectionUtils;
import com.xuexiang.xutil.tip.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import top.lyc829.iot_android.R;
import top.lyc829.iot_android.activity.LoginActivity;
import top.lyc829.iot_android.adapter.ViewPager2Adapter;
import top.lyc829.iot_android.core.BaseFragment;
import top.lyc829.iot_android.fragment.index.IndexFragment;
import top.lyc829.iot_android.fragment.log.LogFragment;
import top.lyc829.iot_android.fragment.profile.ProfileFragment;
import top.lyc829.iot_android.util.Utils;

@Page(name = "MainFragment")
public class MainFragment extends BaseFragment implements BottomNavigationView.OnNavigationItemSelectedListener {

    @BindView(R.id.view_pager)
    ViewPager2 viewPager2;
    @BindView(R.id.bottom_navigation)
    BottomNavigationView bottomNavigation;

    String[] titles = new String[]{"主页", "记录", "我的"};

    private List<Fragment> fragments = new ArrayList<>();

    @Override
    protected TitleBar initTitle() {
        return null;
    }

    public static MainFragment newInstance() {
        return new MainFragment();
    }


    @Override
    protected int getLayoutId() {
        return R.layout.main_fragment;
    }

    @Override
    protected void initViews() {
//        FragmentAdapter<XPageFragment> adapter = new FragmentAdapter<>(getChildFragmentManager());
//        adapter.addFragment(IndexFragment.newInstance(), titles[0]);
//        adapter.addFragment(LogFragment.newInstance(), titles[1]);
//        adapter.addFragment(ProfileFragment.newInstance(), titles[2]);

        fragments.add(IndexFragment.newInstance());
        fragments.add(LogFragment.newInstance());
        fragments.add(ProfileFragment.newInstance());

        viewPager2.setAdapter(new ViewPager2Adapter(getActivity(), fragments));
        //viewPager2.setUserInputEnabled(false);  // 禁止滑动
        viewPager2.setOffscreenPageLimit(fragments.size());

        //viewPager.setOffscreenPageLimit(titles.length - 1);
        //viewPager2.setAdapter(adapter);

        if (Utils.isStringEmpty(SPUtils.getInstance().getString("token"))) {
            Intent intent = new Intent(getActivity(), LoginActivity.class);
            startActivity(intent);
        }

    }

    @Override
    protected void initListeners() {
        bottomNavigation.setOnNavigationItemSelectedListener(this);

        viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels);
                bottomNavigation.getMenu().getItem(position).setChecked(true);
            }
        });

    }


    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        int index = CollectionUtils.arrayIndexOf(titles, item.getTitle());
        if (index != -1) {
            viewPager2.setCurrentItem(index, true);
            return true;
        }
        return false;
    }
}